package com.erp.system.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.erp.common.utils.ShiroUtils;
import com.erp.system.domain.UserDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.erp.system.domain.CompanyDO;
import com.erp.system.service.CompanyService;
import com.erp.common.utils.PageUtils;
import com.erp.common.utils.Query;
import com.erp.common.utils.R;

/**
 * 
 * 
 * @author Eric
 * @email shiliang900713@163.com
 * @date 2019-08-19 18:09:13
 */
 
@Controller
@RequestMapping("/system/company")
public class CompanyController {
	@Autowired
	private CompanyService companyService;
	
	@GetMapping()
	@RequiresPermissions("system:company:company")
	String Company(){
	    return "system/company/company";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("system:company:company")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<CompanyDO> companyList = companyService.list(query);
		int total = companyService.count(query);
		PageUtils pageUtils = new PageUtils(companyList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("system:company:add")
	String add(){
	    return "system/company/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("system:company:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		CompanyDO company = companyService.get(id);
		model.addAttribute("company", company);
	    return "system/company/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("system:company:add")
	public R save( CompanyDO company){
		UserDO userDO = ShiroUtils.getUser();
		company.setGmtCreate(new Date());
		company.setUserNameModified(userDO.getName());
		company.setUserIdModified(userDO.getUserId());
		company.setGmtModified(new Date());
		if(companyService.save(company)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("system:company:edit")
	public R update( CompanyDO company){
		UserDO userDO = ShiroUtils.getUser();
		company.setUserNameModified(userDO.getName());
		company.setUserIdModified(userDO.getUserId());
		company.setGmtModified(new Date());
		companyService.update(company);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("system:company:remove")
	public R remove( Integer id){
		if(companyService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("system:company:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids){
		companyService.batchRemove(ids);
		return R.ok();
	}


	/**
	 * 查询公司列表
	 */
	@GetMapping( "/getCompanyList")
	@ResponseBody
	public List<CompanyDO> getCompanyList(){
		//查询列表数据
		List<CompanyDO> companyList = companyService.list(null);
		return companyList;
	}


	/**
	 * 根据公司ID查询公司信息
	 */
	@GetMapping( "/get/{id}")
	@ResponseBody
	public CompanyDO get(@PathVariable("id") Integer id,Model model){
		//查询列表数据
		CompanyDO companyDO = companyService.get(id);
		return companyDO;
	}

}
